package com.hins.stack;

import java.util.Scanner;

public class ArraryStackDemo {

    public static void main(String[] args) throws Exception {
        ArrayStack arrayStack = new ArrayStack(4);
        String key = "";
        Scanner scanner = new Scanner(System.in);

        boolean loop = true;

        while (loop) {
            System.out.println("show: 表示显示栈");
            System.out.println("exit: 退出程序");
            System.out.println("push: 表示添加数据到栈(入栈)");
            System.out.println("pop: 表示从栈取出数据(出栈)");
            System.out.println("请输入你的选择");
            key = scanner.next();

            switch (key) {
                case "show":
                    arrayStack.list();
                    break;
                case "push":
                    System.out.println("请输入一个参数");
                    int nextInt = scanner.nextInt();
                    arrayStack.push(nextInt);
                    break;
                case "pop":
                    int pop = arrayStack.pop();
                    System.out.printf("弹出一个%d", pop);
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + key);
            }
        }
    }

    static class ArrayStack {
        // 栈的大小
        private int maxSize;
        // 数组，数组模拟栈，数据就放在该数组
        private int[] stack;
        // top表示栈顶，初始化为-1
        private int top = -1;

        public ArrayStack(int maxSize) {
            this.maxSize = maxSize;
            this.stack = new int[maxSize];
        }

        // 栈满
        public boolean isFull() {
            return top == maxSize - 1;
        }

        // 栈空
        public boolean isEmpty() {
            return top == -1;
        }

        // 入栈
        public void push(int value) {
            if (isFull()) {
                System.out.println("栈满了");
                return;
            }
            top++;
            stack[top] = value;
        }

        // 出栈
        public int pop() throws Exception {
            if (isEmpty()) {
                throw new Exception("栈空了，没有数据");
            }
            int value = stack[top];
            top--;
            return value;
        }

        // 查看
        public void list() {
            if (isEmpty()) {
                System.out.println("没有数据");
                return;
            }

            for (int i = top; i >= 0; i--) {
                System.out.printf("stack[%d]=%d", i, stack[i]);
            }
            System.out.println();
        }

        // 查看栈顶的数据
        public int peek() {
            return stack[top];
        }

        //返回运算符的优先级，优先级是程序员来确定, 优先级使用数字表示
        //数字越大，则优先级就越高.
        public int priority(int value) {
            if (value == '*' || value == '/') {
                return 1;
            } else if (value == '+' || value == '-') {
                return 0;
            } else {
                return -1;
            }
        }

        // 判断是不是一个运算符
        public boolean isOper(int value) {
            return value == '/' || value == '*' || value == '-' || value == '+';
        }

        // 计算
        public int cal(int num1, int num2, int oper) {
            int res;
            switch (oper) {
                case '+': res = num1 + num2;break;
                case '-': res = num2 - num1;break;
                case '*': res = num1 * num2;break;
                case '/': res = num2 / num1;break;
                default:
                    throw new IllegalStateException("Unexpected value: " + oper);
            }
            return res;
        }
    }


}
